/*
 * ShareTestPanel.cpp
 *
 *  Created on: 2012-11-5
 *      Author: minhua
 */

#include "ShareTestPanel.h"
#include "../TContext.h"
#include <QMessageBox>

ShareTestPanel::ShareTestPanel(QWidget *parent) : QWidget(parent)
{
	_idEdit = new QLineEdit(this);
	_idEdit->setText("Input Ids");
	_idEdit->setGeometry(10, 10, 150, 30);

	_shareButton = new QPushButton(this);
	_shareButton->setText("Share");
	_shareButton->setGeometry(10, 50, 100, 30);
	connect(_shareButton, SIGNAL(clicked()), this, SLOT(shareButtonClicked()));

	_unshareButton = new QPushButton(this);
	_unshareButton->setText("Unshare");
	_unshareButton->setGeometry(10, 90, 100, 30);
	connect(_unshareButton, SIGNAL(clicked()), this, SLOT(unshareButtonClicked()));

	_requester = TContext::instance()->requesterFactory()->createShareRequester();
	connect(_requester.data(), SIGNAL(shareFinished(bool)), this, SLOT(shareFinished(bool)));
	connect(_requester.data(), SIGNAL(unshareFinished(bool)), this, SLOT(unshareFinished(bool)));
}

ShareTestPanel::~ShareTestPanel()
{
	// TODO Auto-generated destructor stub
}

void ShareTestPanel::shareButtonClicked()
{
	if(_idEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No ids");
	}
	else
	{
		QStringList temp = _idEdit->text().split(",");
		if(temp.size()==1)
		{
			_requester->shareDocument(temp.at(0).toInt());
		}
		else
		{
			QList<int> idList;
			for(int i=1; i<=temp.size(); i++)
			{
				idList.append(temp.at(i-1).toInt());
			}
			_requester->shareDocuments(idList);
		}
	}
}

void ShareTestPanel::unshareButtonClicked()
{
	if(_idEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No ids");
	}
	else
	{
		QStringList temp = _idEdit->text().split(",");
		if(temp.size()==1)
		{
			_requester->unshareDocument(temp.at(0).toInt());
		}
		else
		{
			QList<int> idList;
			for(int i=1; i<=temp.size(); i++)
			{
				idList.append(temp.at(i-1).toInt());
			}
			_requester->unshareDocuments(idList);
		}
	}
}

void ShareTestPanel::shareFinished(bool error)
{
	if(!error)
	{
		QMessageBox::warning(NULL, "Result", "Share Success.");
	}
	else
	{
		QMessageBox::warning(NULL, "Result", "Share Failure.");
	}
}

void ShareTestPanel::unshareFinished(bool error)
{
	if(!error)
	{
		QMessageBox::warning(NULL, "Result", "Unshare Success.");
	}
	else
	{
		QMessageBox::warning(NULL, "Result", "Unshare Failure.");
	}
}

